package com.polaris.live.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.polaris.live.R
import com.polaris.live.databinding.MessageItemCallBinding
import com.polaris.live.databinding.MessageItemGiftBinding
import com.polaris.live.databinding.MessageItemImageBinding
import com.polaris.live.databinding.MessageItemInviteBinding
import com.polaris.live.databinding.MessageItemNoticeBinding
import com.polaris.live.databinding.MessageItemSystemTextBinding
import com.polaris.live.databinding.MessageItemTextBinding
import com.polaris.live.databinding.MessageNotHaveBinding
import com.polaris.live.im.db.entity.MessageEntity
import com.polaris.live.im.sfs.SYSTEM_USER_ID
import com.polaris.live.im.sfs.TYPE_MESSAGE_CALL
import com.polaris.live.im.sfs.TYPE_MESSAGE_GIFT
import com.polaris.live.im.sfs.TYPE_MESSAGE_IMAGE
import com.polaris.live.im.sfs.TYPE_MESSAGE_INVITATION_ROOM
import com.polaris.live.im.sfs.TYPE_MESSAGE_NOTICE
import com.polaris.live.im.sfs.TYPE_MESSAGE_TEXT
import com.polaris.live.ui.message.widget.single_message.MessageCallItemHolder
import com.polaris.live.ui.message.widget.single_message.MessageGiftItemHolder
import com.polaris.live.ui.message.widget.single_message.MessageImageItemHolder
import com.polaris.live.ui.message.widget.single_message.MessageInviteItemHolder
import com.polaris.live.ui.message.widget.single_message.MessageNotHaveItemHolder
import com.polaris.live.ui.message.widget.single_message.MessageNoticeItemHolder
import com.polaris.live.ui.message.widget.single_message.MessageSystemTextItemHolder
import com.polaris.live.ui.message.widget.single_message.MessageTextItemHolder
import com.polaris.live.utils.BitwiseUtils
import com.polaris.live.widget.refresh.BaseBindingViewHolder

/**
 * MessageAdapter
 *
 * @author Created by 半仙 on 2023/9/21/021 19:28
 */
class MessageAdapter(
    val mUserId: Long,
) : BaseQuickAdapter<MessageEntity, BaseViewHolder>(0) {

    private var mAvatar: String? = null
    private var mAvatarFrame: String? = null
    private var mChatBubble: String? = null
    private var mRichs: Long? = null

    init {
        addChildClickViewIds(
            R.id.av_avatar,
            R.id.right_content,
            R.id.left_iv_pic,
            R.id.iv_state,
            R.id.iv_banner,
            R.id.tv_footer,
            R.id.ll_call_left,
            R.id.ll_call_right,
            R.id.go_live_party,
            R.id.go_live_party,
            R.id.go_live_tv,
        )
    }

    override fun getDefItemViewType(position: Int): Int {
        return convertItemType(data[position])
    }

    override fun onCreateDefViewHolder(
        parent: ViewGroup,
        viewType: Int,
    ): BaseBindingViewHolder<*, *> {
        val layoutInflater = LayoutInflater.from(parent.context)
        return when (viewType) {
            ITEM_TEXT -> MessageTextItemHolder(
                this,
                MessageItemTextBinding.inflate(layoutInflater, parent, false)
            )

            ITEM_IMAGE -> MessageImageItemHolder(
                this,
                MessageItemImageBinding.inflate(layoutInflater, parent, false)
            )

            ITEM_GIFT -> MessageGiftItemHolder(
                this,
                MessageItemGiftBinding.inflate(layoutInflater, parent, false)
            )

            ITEM_CALL -> MessageCallItemHolder(
                this,
                MessageItemCallBinding.inflate(layoutInflater, parent, false)
            )

            ITEM_NOTICE -> MessageNoticeItemHolder(
                this,
                MessageItemNoticeBinding.inflate(layoutInflater, parent, false)
            )

            ITEM_SYSTEM_TEXT -> MessageSystemTextItemHolder(
                this,
                MessageItemSystemTextBinding.inflate(layoutInflater, parent, false)
            )

            ITEM_NOT_HAVE -> MessageNotHaveItemHolder(
                this,
                MessageNotHaveBinding.inflate(layoutInflater, parent, false)
            )

            ITEM_INVITE_TEXT -> MessageInviteItemHolder(
                this,
                MessageItemInviteBinding.inflate(layoutInflater, parent, false)
            )

            else -> throw IllegalArgumentException("Invalid view type: $viewType")
        }
    }

    override fun convert(holder: BaseViewHolder, item: MessageEntity) {
        when (holder) {
            is MessageTextItemHolder -> holder.bind(item)
            is MessageImageItemHolder -> holder.bind(item)
            is MessageGiftItemHolder -> holder.bind(item)
            is MessageCallItemHolder -> holder.bind(item)
            is MessageNoticeItemHolder -> holder.bind(item)
            is MessageNotHaveItemHolder -> holder.bind(item)
            is MessageSystemTextItemHolder -> holder.bind(item)
            is MessageInviteItemHolder -> holder.bind(item)
        }
    }

    /**
     * 设置装饰数据
     */
    fun setDecorationData(avatar: String?, avatarFrame: String?, chatBubble: String?) {
        if (mAvatar != avatar || mAvatarFrame != avatarFrame || mChatBubble != chatBubble) {
            mAvatar = avatar
            mAvatarFrame = avatarFrame
            mChatBubble = chatBubble
            notifyDataSetChanged()
        }
    }

    fun setRichs(richs: Long) {
        mRichs = richs
        notifyDataSetChanged()
    }

    fun getAvatar(): String? {
        return mAvatar
    }

    fun getRichs(): Long? {
        return mRichs
    }

    fun getAvatarFrame(): String? {
        return mAvatarFrame
    }

    fun getChatBubble(): String? {
        return mChatBubble
    }

    companion object {

        private const val ITEM_TEXT = 0
        private const val ITEM_GIFT = 1
        private const val ITEM_IMAGE = 2
        private const val ITEM_CALL = 3
        private const val ITEM_NOTICE = 4
        private const val ITEM_SYSTEM_TEXT = 10
        private const val ITEM_INVITE_TEXT = 11 //邀请进房消息
        private const val ITEM_NOT_HAVE = 100

        private fun convertItemType(item: MessageEntity): Int {
            val type = item.type
            return if (isGift(type)) {
                ITEM_GIFT
            } else if (isCall(type)) {
                ITEM_CALL
            } else if (isNotice(type)) {
                ITEM_NOTICE
            } else if (isImage(type)) {
                ITEM_IMAGE
            } else if (isText(type)) {
                if (item.sender == SYSTEM_USER_ID) {
                    ITEM_SYSTEM_TEXT
                } else {
                    ITEM_TEXT
                }
            } else if (isInvitation(type)) {
                ITEM_INVITE_TEXT
            } else {
                ITEM_NOT_HAVE
            }
        }

        private fun isGift(type: Long) = BitwiseUtils.isBitwise(type, TYPE_MESSAGE_GIFT)

        private fun isCall(type: Long) = BitwiseUtils.isBitwise(type, TYPE_MESSAGE_CALL)

        private fun isNotice(type: Long) = BitwiseUtils.isBitwise(type, TYPE_MESSAGE_NOTICE)

        private fun isImage(type: Long) = BitwiseUtils.isBitwise(type, TYPE_MESSAGE_IMAGE)

        private fun isText(type: Long) = BitwiseUtils.isBitwise(type, TYPE_MESSAGE_TEXT)

        private fun isInvitation(type: Long) = BitwiseUtils.isBitwise(type, TYPE_MESSAGE_INVITATION_ROOM)

    }
}